/***************************************************************************
 *  WlanSavvy
 *  Copyright  2007  Deepak Manoharan
 *  Email deepakm@gmail.com
 *  http://wlansavvy.googlecode.com/
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "connectionpanel.h"

#define NUM_GRAPH_PTS 102

static GtkWidget * lbl_network;
static GtkWidget * lbl_essid;
static GtkWidget * lbl_accesspoint;
static GtkWidget * lbl_ipadrmask;
static GtkWidget * lbl_frequency;
static GtkWidget * lbl_bitrate;
static GtkWidget * lbl_txpower;
static GtkWidget * lbl_encryption;
static GtkWidget * lbl_mode;
static GtkWidget * lbl_sensitivity;
static GtkWidget * lbl_tx;
static GtkWidget * lbl_rx;
static GtkWidget * lbl_rxinvalid;
static GtkWidget * lbl_beacon;
static GtkWidget * lbl_invalidmisc;
static GtkWidget * lbl_txretries;
static GtkWidget * lbl_rts;
static GtkWidget * lbl_frag;

static GtkWidget * pgbar_link;
static GtkWidget * pgbar_signal;
static GtkWidget * pgbar_noise;
static GtkWidget * pgbar_snr;
static GtkWidget * da_history;

/*TODO Clean em up properly*/
static GdkGC * gc_signal;
static GdkGC * gc_noise;
static GdkGC * gc_snr;
static GdkGC * gc_graphlines;

static GdkColor clr_signal;
static GdkColor clr_noise;
static GdkColor clr_snr;
static GdkColor clr_graphlines;

static PangoLayout *pl_signal;
static PangoLayout *pl_noise;
static PangoLayout *pl_snr;


static GdkPoint pts_signal[NUM_GRAPH_PTS];
static GdkPoint pts_noise[NUM_GRAPH_PTS];
static GdkPoint pts_snr[NUM_GRAPH_PTS];


void refresh_history_graph( GtkWidget *widget)
{
	gint i;
	gint width, height;
	gdk_drawable_get_size(widget->window, &width, &height);

  	gdk_draw_rectangle(widget->window,
        widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
		TRUE,
        0,0,width,height);
	
	for(i=0;i<height;i+=20){
		gdk_draw_line(widget->window,gc_graphlines,
					  0,i,width,i);
	}

  	gdk_draw_lines(widget->window, gc_signal, &pts_signal[0],NUM_GRAPH_PTS);
  	gdk_draw_lines(widget->window, gc_noise, &pts_noise[0],NUM_GRAPH_PTS);
  	gdk_draw_lines(widget->window, gc_snr, &pts_snr[0],NUM_GRAPH_PTS);
	
	gdk_draw_layout(widget->window, gc_signal,5,2,pl_signal);
	gdk_draw_layout(widget->window, gc_noise,285,2,pl_noise);
	gdk_draw_layout(widget->window, gc_snr,575,2,pl_snr);
}


void init_graph_points()
{
	int i=0;
	for(i=0;i<NUM_GRAPH_PTS;i++){
		pts_signal[i].x=pts_noise[i].x=pts_snr[i].x=(6*i);
		pts_signal[i].y=pts_noise[i].y=pts_snr[i].y=110;
	}
	refresh_history_graph(da_history);
}

void insert_graph_points(gint signal, gint noise, gint snr)
{
	int i=0;
	for(i=0;i<(NUM_GRAPH_PTS-1);i++){
		pts_signal[i].y = pts_signal[i+1].y;
		pts_noise[i].y = pts_noise[i+1].y;
		pts_snr[i].y = pts_snr[i+1].y;
	}

	pts_signal[NUM_GRAPH_PTS-1].y = 110-signal;
	pts_noise[NUM_GRAPH_PTS-1].y = 110-noise;
	pts_snr[NUM_GRAPH_PTS-1].y = 110-snr;
	refresh_history_graph(da_history);
}

void init_connection_panel()
{
	/**
	 * Retrive all widgets in advance, we refresh very often
 	 * lets not load glade	 
	 **/
	lbl_network = glade_xml_get_widget (xml, "lbl_network");
	lbl_essid = glade_xml_get_widget (xml, "lbl_essid");
	lbl_accesspoint = glade_xml_get_widget (xml, "lbl_accesspoint");
	lbl_ipadrmask = glade_xml_get_widget (xml, "lbl_ipadrmask");
	lbl_frequency = glade_xml_get_widget (xml, "lbl_frequency");
	lbl_bitrate = glade_xml_get_widget (xml, "lbl_bitrate");
	lbl_txpower = glade_xml_get_widget (xml, "lbl_txpower");
	lbl_encryption = glade_xml_get_widget (xml, "lbl_encryption");
	lbl_mode = glade_xml_get_widget (xml, "lbl_mode");
	lbl_sensitivity = glade_xml_get_widget (xml, "lbl_sensitivity");
	lbl_tx = glade_xml_get_widget (xml, "lbl_tx");
	lbl_rx = glade_xml_get_widget (xml, "lbl_rx");
	lbl_rxinvalid = glade_xml_get_widget (xml, "lbl_rxinvalid");
	lbl_beacon = glade_xml_get_widget (xml, "lbl_beacon");
	lbl_invalidmisc = glade_xml_get_widget (xml, "lbl_invalidmisc");
	lbl_txretries = glade_xml_get_widget (xml, "lbl_txretries");
	lbl_rts = glade_xml_get_widget (xml, "lbl_rts");
	lbl_frag = glade_xml_get_widget (xml, "lbl_frag");

	pgbar_link = glade_xml_get_widget (xml, "pgbar_link");
	pgbar_signal = glade_xml_get_widget (xml, "pgbar_signal");
	pgbar_noise = glade_xml_get_widget (xml, "pgbar_noise");
	pgbar_snr = glade_xml_get_widget (xml, "pgbar_snr");
	pgbar_snr = glade_xml_get_widget (xml, "pgbar_snr");
	da_history = glade_xml_get_widget (xml, "da_history");

	gdk_color_parse("green",&clr_signal);
	gdk_color_parse("red",&clr_noise);
	gdk_color_parse("yellow",&clr_snr);
	gdk_color_parse("grey20",&clr_graphlines);

	gc_signal = gdk_gc_new(da_history->window);
	gc_noise = gdk_gc_new(da_history->window);
	gc_snr = gdk_gc_new(da_history->window);
	gc_graphlines = gdk_gc_new(da_history->window);

	gdk_gc_set_rgb_fg_color(gc_signal, &clr_signal);
	gdk_gc_set_rgb_fg_color(gc_noise, &clr_noise);
	gdk_gc_set_rgb_fg_color(gc_snr, &clr_snr);
	gdk_gc_set_rgb_fg_color(gc_graphlines, &clr_graphlines);

	gdk_gc_set_line_attributes (gc_signal, 1, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
	gdk_gc_set_line_attributes (gc_noise, 1, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
	gdk_gc_set_line_attributes (gc_snr, 1, GDK_LINE_SOLID, GDK_CAP_BUTT, GDK_JOIN_MITER);
	gdk_gc_set_line_attributes (gc_graphlines, 1, GDK_LINE_ON_OFF_DASH, GDK_CAP_BUTT, GDK_JOIN_MITER);


	pl_signal = gtk_widget_create_pango_layout (da_history,"signal");
	pl_noise = gtk_widget_create_pango_layout (da_history,"noise");
	pl_snr = gtk_widget_create_pango_layout (da_history,"snr");

	init_graph_points();
}

void set_lbl_interface(gchar *if_name)
{
	gtk_label_set_text(GTK_LABEL(lbl_network),if_name);
}

void set_lbl_essid(gchar *essid)
{
	gtk_label_set_text(GTK_LABEL(lbl_essid),essid);
}

void set_lbl_accesspoint(gchar *ap)
{
	gtk_label_set_text(GTK_LABEL(lbl_accesspoint),ap);
}

void set_lbl_ipadrmask(gchar *ipadrmask)
{
	gtk_label_set_text(GTK_LABEL(lbl_ipadrmask),ipadrmask);
}

void set_lbl_frequency(gchar *frequency)
{
	gtk_label_set_text(GTK_LABEL(lbl_frequency),frequency);
}

void set_lbl_bitrate(gchar *bitrate)
{
	gtk_label_set_text(GTK_LABEL(lbl_bitrate),bitrate);
}

void set_lbl_txpower(gchar *txpower)
{
	gtk_label_set_text(GTK_LABEL(lbl_txpower),txpower);
}

void set_lbl_encryption(gchar *encryption)
{
	gtk_label_set_text(GTK_LABEL(lbl_encryption),encryption);
}

void set_lbl_mode(gchar *mode)
{
	gtk_label_set_text(GTK_LABEL(lbl_mode),mode);
}

void set_lbl_sensitivity(gchar *sensitivity)
{
	gtk_label_set_text(GTK_LABEL(lbl_sensitivity),sensitivity);
}

void set_lbl_tx(gchar *tx)
{
	gtk_label_set_text(GTK_LABEL(lbl_tx),tx);
}

void set_lbl_rx(gchar *rx)
{
	gtk_label_set_text(GTK_LABEL(lbl_rx),rx);
}

void set_lbl_rxinvalid(gchar * lbl)
{
	gtk_label_set_text(GTK_LABEL(lbl_rxinvalid),lbl);
}

void set_lbl_beacon(gchar * lbl)
{
	gtk_label_set_text(GTK_LABEL(lbl_beacon),lbl);
}

void set_lbl_invalidmisc(gchar * lbl)
{
	gtk_label_set_text(GTK_LABEL(lbl_invalidmisc),lbl);
}

void set_lbl_txretries(gchar * lbl)
{
	gtk_label_set_text(GTK_LABEL(lbl_txretries),lbl);
}

void set_lbl_rts(gchar * lbl)
{
	gtk_label_set_text(GTK_LABEL(lbl_rts),lbl);
}

void set_lbl_frag(gchar * lbl)
{
	gtk_label_set_text(GTK_LABEL(lbl_frag),lbl);
}

void set_pgbar_link_text(gchar * linkval)
{
	gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgbar_link),linkval);
}

void set_pgbar_link_fraction(gdouble linkfraction)
{
	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pgbar_link),linkfraction);
}

void set_pgbar_signal_text(gchar * signal)
{
	gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgbar_signal),signal);
}

void set_pgbar_signal_fraction(gdouble signal)
{
	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pgbar_signal),signal);
}

void set_pgbar_noise_text(gchar * noise)
{
	gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgbar_noise),noise);
}

void set_pgbar_noise_fraction(gdouble noise)
{
	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pgbar_noise),noise);
}

void set_pgbar_snr_text(gchar * snr)
{
	gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgbar_snr),snr);
}

void set_pgbar_snr_fraction(gdouble snr)
{
	gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pgbar_snr),snr);
}

gboolean da_history_expose_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
	refresh_history_graph(widget);
	return TRUE;
}

gboolean da_history_configure_event_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
{
	refresh_history_graph(widget);
	return TRUE;
}



